/**
 *  Copyright Matt Silbernagel 2010
 *  
 *	This file is part of MobilePrinting.
 *
 *	MobilePrinting is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MobilePrinting is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MobilePrinting.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.uc.labs.MobilePrinting.GUI;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URISyntaxException;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import static edu.uc.labs.MobilePrinting.Configurator.*;

/**
 * @author Matt Silbernagel
 *
 */
public class AboutPanel extends JPanel implements ActionListener {

	private static final long serialVersionUID = 2918563296010171818L;
	
	/**
	 * Constructor for AboutPanel
	 * @param FrameTitle What the display name should be
	 */
	public AboutPanel( String FrameTitle ){
		frameTitle = FrameTitle;
		initComponents();
		
	}
	
	/**
	 * Initialize the components
	 */
	public void initComponents() {		
		
		// Set up the buttons
		f = new FeedbackButton();
		f.addActionListener(this);
		ok = new OkButton();
		ok.addActionListener(this);
		
		northPanel = new NorthPanel();
		southPanel = new JPanel();
		buttonPanel = new ButtonPanel();
		creditsPanel = new CreditsPanel();
		
		// Setup the north panel...
		ImageIcon icon = new ImageIcon(ICONS + DELIM + MAINIMAGE);
		JLabel label = new JLabel();
		label.setIcon(icon);
		northPanel.add(label, BorderLayout.WEST);
		northPanel.add(creditsPanel, BorderLayout.CENTER);
		
		// Add the credits and button panels to 
		// the south panel
		southPanel.setLayout(new BorderLayout());
		southPanel.add(buttonPanel, "Center");
		
		setLayout(new BorderLayout());
		add(northPanel, BorderLayout.CENTER);
		add(southPanel, BorderLayout.PAGE_END);
		setSize(600, 600);
		
		mainFrame = new JFrame(frameTitle);
		Container contentPane = mainFrame.getContentPane();
		contentPane.add(this);
		mainFrame.setLocation(400, 200);
		mainFrame.pack();
		mainFrame.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Command obj = (Command) e.getSource();
		obj.execute();	
	}
	
	JPanel northPanel;
	JPanel southPanel;
	JPanel creditsPanel;
	JPanel buttonPanel;
	FeedbackButton f;
	OkButton ok;
	JFrame mainFrame;
	String frameTitle;
	
	/**
	 * The class that builds and executes the Feedback Button and
	 * @author Matt Silbernagel
	 *
	 */
	public class FeedbackButton extends JButton implements Command {

		private static final long serialVersionUID = 1139248633556787241L;
		String text = "Feedback";
		
		public FeedbackButton(){
			this.setText(text);
		}
		
		@Override
		public void execute() {
			//JOptionPane.showMessageDialog(this, "Clicked the Feedback Button: ");
			if( !java.awt.Desktop.isDesktopSupported() ) {
				LOGGER.error("Can't open link because can't determine default browser");
			} else {
				java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
				if( !desktop.isSupported( java.awt.Desktop.Action.BROWSE ) ) {
		            LOGGER.error( "Desktop doesn't support the browse action (fatal)" );		            
		        } else {
		        	try {
						java.net.URI url = new java.net.URI( "http://" + HTTP_SERVER + "/feedback");
						desktop.browse(url);
					} catch (URISyntaxException e) {
						LOGGER.error("The url is invalid");
					} catch (IOException e) {
						LOGGER.error("Couldn't open the browser etc...");
					}
		        }
			}
				
		}
	}

	/**
	 * The class that builds and executes the Ok Button and
	 * @author Matt Silbernagel
	 *
	 */
	public class OkButton extends JButton implements Command {

		private static final long serialVersionUID = -2985090634174376910L;
		String text = "Close";
		
		public OkButton(){
			this.setText(text);
		}
		
		@Override
		public void execute() {
			mainFrame.setVisible(false);
		}
		
	}
	
	/**
	 * The class that builds the ButtonPanel
	 * @author Matt Silbernagel
	 *
	 */
	public class ButtonPanel extends JPanel {
		
		private static final long serialVersionUID = -5291872513241297221L;

		public ButtonPanel(){
			
			setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
			setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
			add(Box.createHorizontalGlue());
			add(f);
			add(Box.createRigidArea(new Dimension(10, 0)));
			add(ok);
		}
	}
	
	/**
	 * The class that builds the NorthPanel
	 * @author Matt Silbernagel
	 *
	 */
	public class NorthPanel extends JPanel {

		private static final long serialVersionUID = 5718795294115831391L;

		public NorthPanel(){
			setBorder(BorderFactory.createEmptyBorder(0, 25, 10, 25));
			setPreferredSize(new Dimension(600, 200));
			setLayout(new BorderLayout());
		}
	}
	
	
	public class CreditsPanel extends JPanel {

		private static final long serialVersionUID = 383994597420583815L;

		public CreditsPanel(){
			setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
			//setBorder(BorderFactory.createLineBorder(Color.BLACK));
			setPreferredSize(new Dimension(450, 200));
			JLabel header = new JLabel();
			header.setFont(new Font("Serif", Font.BOLD, 28));		
			header.setText("MobilePrinting");
			JLabel text = new JLabel();
			text.setFont(new Font("Serif",Font.ITALIC, 11));
			text.setText("<html> This application was developed for The University of Cincinnati " +
					               "by Matt Silbernagel. All graphics were developed by Michael Mahoney." +
					               "Please click the feedback button and tell us what you think </html>");
			JLabel version = new JLabel("Version: " + VERSION);
			version.setFont(new Font("Serif",Font.PLAIN, 12));
			
			JLabel copyright = new JLabel("Copyright Matt Silbernagel 2010");
			copyright.setFont(new Font("Serif",Font.PLAIN, 12));
			
			add(header);
			add(Box.createRigidArea(new Dimension(0, 5)));
			add(version);
			add(Box.createRigidArea(new Dimension(0, 25)));
			add(text);
			add(Box.createRigidArea(new Dimension(0, 25)));
			add(copyright);
		}
	}

}
